<template>
	<view class="wrapper">
		<button class="login" v-if="getmsg" lang="zh_CN" open-type="getUserInfo" @getuserinfo="getuserinfo" type="primary">开启约会赚钱之旅</button>
		<view v-if="pos" class="pos">
			<view class="alert">
				<view class="txt">
					拒绝地理位置的授权，系统无法获取你附近的活动和用户
				</view>
				<button open-type="openSetting" @opensetting="opensetting" class="go">去授权</button>
			</view>
		</view>
	</view>
</template>

<script>
	import common from "../../common/network.js";
	import app from "../../App.vue";
	export default {
		data(){
			return{
				getmsg:false,pos:false
			}
		},
		onLoad:function(options){
			var that = this;
			if(options.btn!=undefined){
				this.getmsg = true;
			}else{
				this.getmsg = false;
			}
		},
		onShow:function(){
			this.location();
		},
		methods:{
			location:function(){
				var that = this;
				uni.getLocation({
					success:function(res){
						that.pos = false;
					},
					fail:function(){
						that.pos = true;
					}
				})
			},
			opensetting:function(e){
				if(e.detail.authSetting['scope.userLocation']==true){
					this.pos = false;
				}else{
					this.pos = true;
				}
			},
			getuserinfo:function(e){
				var that = this;
				var userInfo = e.detail.userInfo;
				console.log(userInfo)
				if(userInfo!=undefined){
					wx.showLoading({
						title: '登录中...',
						mask:true
					})
					this.getmsg = false;
					wx.login({
						provider: 'weixin',
					    success: function (res) { 
							if (res.code) {
								console.log(res)
								that.loginuser(res.code, userInfo)
							} else {
							    console.log('获取用户登录态失败！' + res.errMsg)
							}
						}
					});
				}
			},
			loginuser:function(data, usermsg){
				let user = {
					"type": "login",
					"code": data,
					"userInfo": JSON.stringify(usermsg),
				}
				common.network.request(user,function(res){
					console.log(res.userInfo)
					common.network.setStorage('userinfo',res.userInfo,function(){
						setTimeout(function () {
							wx.hideLoading();
							wx.reLaunch({
								url: '/pages/index/index',
							})
						}, 2000)
					})
				})
			},
		}
	}
</script>

<style scoped>
	.wrapper{
		height: 100vh;
		background: url('https://yp.6vx.com/data/sys_img/other_img/beginBg.jpg') no-repeat center;
		background-size: cover;
	}
	.wrapper .login{
		width: 80%;
		position: fixed;
		bottom: 14%;
		left: 10%;
		height: 100px;
		line-height: 96px;
		background: url("https://yp.6vx.com/data/sys_img/other_img/loginbtn.png") no-repeat center;
		background-size: 100% 100%;
		box-shadow: 0 10px 10px rgba(0,0,0,0.2);
		font-size: 34px;
	}
	.pos{
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		background: rgba(0,0,0,0.6);
	}
	.alert{
		background: #fff;
		border-radius: 10px;
		width: 80%;
		position: absolute;
		top: 40%;
		left: 10%;
	}
	.alert .txt{
		font-size: 30px;
		color: #A1A2A4;
		text-align: center;
		padding: 40px;
		line-height: 50px;
		padding-bottom: 20px;
		border-bottom: 1px solid #DBDCDE;
	}
	.alert .go{
		line-height: 90px;
		height: 90px;
		color: #4C1BD6;
		font-size: 32px;
		background: #fff;
	}
</style>
